package com.powerunion.datacollection.web.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.datacollection.model.MaintblData;
import com.powerunion.datacollection.model.query.ApproveTargetsDataParams;
import com.powerunion.datacollection.service.ApproveTargetsDataManager;
import com.powerunion.datacollection.service.MaintblDataManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class ApproveTargetsDataAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(
			ApproveTargetsDataAction.class);
	private ApproveTargetsDataManager atdm = null;

	public ApproveTargetsDataAction() {
		super();
		this.atdm = new ApproveTargetsDataManager();
	}

	public void finalize() throws Throwable {
		this.atdm = null;
		super.finalize();
	}
	
	public ActionForward save4approve(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		String targetsData = request.getParameter("targetsData");
		String documentId = request.getParameter("documentId");
		String submiUser = request.getRemoteUser();
		String procId = request.getParameter("procId");
		int iTemplate = Integer.parseInt(request.getParameter("iTemplate"));
		String actId = request.getParameter("actId");
		String mainTblFormValuesStr = request.getParameter("mainTblFormValuesStr");
		
		ApproveTargetsDataParams params = new ApproveTargetsDataParams();
		params.setFormId(documentId);
		params.setUpUser(submiUser);
		params.setProcId(procId);
		params.setIAmalgamation(iTemplate);
		params.setSubmiUser(submiUser);
		params.setAwId(actId);
		params.setSwId(actId);
		
		boolean flag = false;
		boolean mainFlag = false;
		response.setContentType("text/json;charset=utf-8");
		Transaction maintblDatatransaction = null;
		try {
			maintblDatatransaction = TransactionHelper.getTransaction();
			maintblDatatransaction.begin();
			logger.debug("指标报送信息定义:" + targetsData);
		
			//保存主表报送区数据
			if(!StringUtils.isEmpty(mainTblFormValuesStr)){
				MaintblDataManager mdm = new MaintblDataManager();
				MaintblData data = new MaintblData();
				data.setFormId(documentId);
				data.setActId(actId);
				data.setProcId(procId);
				data.setSubmiUser(submiUser);
				mainFlag = mdm.save(data, mainTblFormValuesStr);
			}

			maintblDatatransaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				maintblDatatransaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				maintblDatatransaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		
		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("指标报送信息定义:" + targetsData);
			
			flag = this.atdm.save(targetsData, params);

			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		
		if (flag && mainFlag) {
			response.getWriter().write("{success:true}");
		} else {
			response.getWriter().write("{failure:true}");
		}

		return null;
	}
	
	
}
